
 apiready = function () {
 
       headerstatus();
       var wx = api.require('wx');
        wx.isInstalled(function(ret, err){
            if(!ret.installed){
                 var wx_ = $api.byId('wx');
                 $api.css(wx_,'display:none');
                
            }else{
            	$api.css($api.byId('other'),'display:block');
            }
        });
           var qq = api.require('qq');
            qq.installed(function(ret,err){
	            if(!ret.status){
	               
	              var qq_ = $api.byId('qq');
                 $api.css(qq_,'display:none');
					
	            }else{
            	$api.css($api.byId('other'),'display:block');
            }
        });
        
        		
        	
 
    };
    function checkwx(){
    
    }
    function checkqq(){}
   /* function openWin(name){
    	api.openWin({
    		name: name,
        	url: 'html/'+name+'.html',
        	opaque: true,
        delaye:200,
        vScrollBarEnabled:false
    		
    	});

    }*/
    /*function toRegister(){
        api.openWin({
            name:'register',
            url:'win_register.html'
        })
    }*/

    function checkUserCode(username,password){
       if(username!=''&&password!=''){
            var t = /^1[3|4|5|7|8]\d{9}$/;
            
            var p1 = /^([\x00-\xff]){6,20}$/;
            var p2 = /^\S{6,20}$/;
           var t = t.test(username);
           
       
           var p1 = p1.test(password);
           var p2 = p2.test(password);
         
           if($api.getStorage('registrationId')){
				var rid = $api.getStorage('registrationId')
			}else{
				var ajpush = api.require('ajpush');
						ajpush.getRegistrationId(function(ret) {
							if(ret.id){
								var rid = ret.id;	
							}else{
								var rid = null;	
							}
											
						});
			}
        
           if(t==false&&p1==false&&p2==false){
                toast("账号密码不正确");
                return false;
           }else{
            
                api.ajax({
                    url: 'http://wap.jqmao.cn/checkMember',
                    method: 'post',   
                    data:{
                        values: {username: username,password:password,rid:rid}
                    }
                },function(ret,err){
               
                    if(ret){
                    
                        if (ret.member) {
                        
                            $api.setStorage('user',ret.member);

                            var user = $api.getStorage('user');
                            if(user){
                            
							var username = $api.getStorage('user').username;
								
								var	uid = $api.getStorage('user').uid;
							

                                api.execScript({
                                    name:'root',
                                    frameName: 'user',
                                    script: 'userReload()'
                                });
                                //api.closeWin();
                         
                                        api.closeWin();
                                
                                //openWin('user');
                               // api.closeFrame();
                            }
                        }else{
                            toast("账号或密码不正确");
                        }
                    }else{
                        //alert(JSON.stringify(err))
                    }
                })
           }

       }else{
            toast("账号或密码不能为空");
            return false;
       }
    }
    function login(){
        api.showProgress();
        var username=$("#username").val();
        var password=$("#password").val();
        
        checkUserCode(username,password);

        api.hideProgress();
       
    }
    //授权登录开始

   /*微信授权登录*/
   var wxAuth = function(){
        var wx = api.require('wx');
        wx.isInstalled(function(ret, err){
            if(ret.installed){
              
                 wx_login();
            }else{
                alert('当前设备未安装微信客户端');

            }
        });
        function wx_login(){
          
            wx.auth({
                apiKey: 'wx1e7bc90543efac5d'
            }, function(ret, err){ 
                
                if(ret.status){
                   	var code = ret.code;
                   	getToken(code);
                    
                }else{
                //-1（未知错误），
                //0（成功，用户同意）
                //1 (用户取消)
                //2 (用户拒绝授权)
                //3 (当前设备未安装微信客户端)
                    if(err.code==-1){
                    	toast('未知错误');
                    }else if(err.code==1){
                    	toast('已取消授权');
                    }else if(err.code==2){
                    	toast('已拒绝授权');
                    }else{
                    	toast('当前设备未安装微信客户端');
                    }
                }
            });
        }
        
        function getToken(code){
        	
				wx.getToken({
				    apiKey: '',
				    apiSecret: '',
				    code: code
				},function(ret, err){ 
				    if(ret.status){
				    	
				    	var openid = ret.openId; 
				    	var accessToken = ret.accessToken;
				    	var dynamicToken = ret. dynamicToken;
				    	getUserInfo(openid,accessToken,dynamicToken);
				        
				    }else{
				        
				        //数字类型；
		                //错误码：
		                //-1（未知错误），
		                //0 （成功）
		                //1 (apiKey值为空或非法)
		                //2 (apiSecret值为空或非法)
		                //3 (code值为空或非法)
		                //4 (网络超时)
		                if(err.code==4){
		                	toast('网络超时')
		                }else if(err.code==-1){
		                	toast('未知错误')
		                }else{
		                	toast('code值为空或非法')
		                }
				    }
				});
        }
        function getUserInfo(openid,accessToken,dynamicToken){
        	
			wx.getUserInfo({
			    accessToken: accessToken,
			    openId: openid
			}, function(ret,err){ 
			    if(ret.status){
			    //alert(JSON.stringify(ret));
			    $api.setStorage('authorize',ret);
			    
			    	var unionid = ret.unionid;
			    	//alert($api.getStorage('registrationId'));
			       	if($api.getStorage('registrationId')){
						var rid = $api.getStorage('registrationId');
					}else{
						var ajpush = api.require('ajpush');
						ajpush.getRegistrationId(function(ret) {
							if(ret.id){
								var rid = ret.id;	
							}else{
								var rid = null;	
							}
											
						});
					}
                   
			        api.ajax({
                        url:'http://wap.jqmao.cn/save_wx_info',
                        method:'post',
                        data:{
                            values:{unionid: unionid,rid: rid}
                        }
                        },function(ret,err){
                            if(ret){
                                if(ret.member){
                                    $api.setStorage('user',ret.member);

                            		var user = $api.getStorage('user');
		                            if(user){
		                           
									var username = $api.getStorage('user').username;
									
									var	uid = $api.getStorage('user').uid;
										
		                                api.execScript({
		                                    name:'root',
		                                    frameName: 'user',
		                                    script: 'userReload()'
		                                });
		                                //api.closeWin();
		                         
		                                 api.closeWin();               
		                                //openWin('user');
		                               // api.closeFrame();
		                            }
                                }else{
                                	setTimeout(openHeader("binding"),0);
                                }
                                
                            }else{
                                //alert(JSON.stringify(err))
                                
				        			toast("请求超时")
				        			return false;
				        		
                            }
                        })
			    
			   		
			    
			        //alert(JSON.stringify(ret));
			        //var openid = ret.openid;
			        
			       }else{
			        //alert(err.code);
			        if(err.code==1){
			        	refreshToken(dynamicToken);
			        }
			    }
			});
        }
        function refreshToken(dynamicToken){
        	wx.refreshToken({
			    apiKey: '',
			    dynamicToken: dynamicToken
			},function(ret,err){ 
			    if(ret.status){
			        
			        var openid = ret.openId;
			        var accessToken = ret.accessToken;
			        var dynamicToken = ret. dynamicToken; 
			        getUserInfo(openid,accessToken,dynamicToken)
			    }else{
			        alert(err.code);
			    }
			});
        }
    }
	/*微信登录结束*/
    /*qq授权登录*/
    var qq = function(){
        var qq = api.require('qq');

        //判断客户端安装状态
        qq_check_installed();
        //login
        


        /*检查qq是否安装*/
        function qq_check_installed(){
            qq.installed(function(ret,err){
	            if(ret.status){
	               
	               qq_login();
	
	            }else{
	                api.alert({msg: "没有安装qq客户端"});
	            } 
        	});
       
        }
        /*login*/
        function qq_login(){
          qq.login(function(ret,err){
            var openid = ret.openId;
            
            qq_getInfo(openid);
          });
         
        }
        /*getinfo*/
        function qq_getInfo(openid){
            qq.getUserInfo(function(ret,err) {
               if (ret.status) {
              
                var img = ret.info['figureurl_qq_1'];
                var username = ret.info['nickname'];
                var openId = openid;
                ret.openid= openId;
                
                ret.nickname =ret.info['nickname'];
              	ret.headimgurl =ret.info['figureurl_qq_1'];
              	ret.info='';
              	if($api.getStorage('registrationId')){
					var rid = $api.getStorage('registrationId')
				}else{
					var ajpush = api.require('ajpush');
						ajpush.getRegistrationId(function(ret) {
							if(ret.id){
								var rid = ret.id;	
							}else{
								var rid = null;	
							}
											
						});
				}
		
               
               	$api.setStorage('authorize',ret);
                    api.ajax({
                        url:'http://wap.jqmao.cn/save_qq_info',
                        method:'post',
                        data:{
                            values:{openid: openId,img: img,username: username,rid: rid}
                        }
                        },function(ret,err){
                            if(ret){
                            
                                if(ret.member){
                                    $api.setStorage('user',ret.member);

                            var user = $api.getStorage('user');
                            if(user){
                           
							var username = $api.getStorage('user').username;
							
							var	uid = $api.getStorage('user').uid;
						


                                api.execScript({
                                    name:'root',
                                    frameName: 'user',
                                    script: 'userReload()'
                                });
                                //api.closeWin();
                         
                                        api.closeWin();
                                
                                //openWin('user');
                               // api.closeFrame();
                            }
                                }else{
                                	setTimeout(openHeader("binding"),0);
                                }
                                
                            }else{
                                toast("请求超时")
				        		return false;
                                /*//alert(JSON.stringify(err));*/
                            }
                        })

               }else{
                  api.alert({msg:err.msg});
               }
             });
            
        }
        
    }
     

  


    /*新浪微博登录*/
    function wb(){

    }
    //授权登录结束